Running Kanzi applications in multithread environments

Kanzi is not thread safe, because it uses non thread safe libraries such as OpenGL, and contains subsystems that do not protect against concurrent access.

The following Kanzi components do not offer protection against concurrent access:

However, it is possible to run Kanzi in multithread environments. Kanzi does not have a global state, so you can use independent components in different threads. Kanzi also provides a multithreading API with implementations for supported platforms.

Main components of Kanzi, such as gestures and the message queue, are executed in a single-thread manner. In practical applications it is often necessary to load resources from a local file system or download from the Internet concurrently. You can do this with functionality exposed in the kzsThread class. You can then pass the loaded resources to Kanzi by the main thread.

The system memory manager is thread safe, but for efficient memory management, create a separate pooled memory manager for each thread:

struct KzcMemoryManager* systemMemoryManager;
struct KzcMemoryManager* memoryManager;

result = kzcMemoryManagerCreateSystemManager(&systemMemoryManager);
kzsErrorForward(result);

result = kzcMemoryManagerCreatePooledManager(systemMemoryManager, 1, poolSize, &memoryManager);
kzsErrorForward(result);

As an alternative to creating another instance of the systemMemoryManager, you can query for the existing instance from the project. The memory managers should be freed when the thread finishes:

result = kzcMemoryManagerDelete(memoryManager);
kzsErrorForward(result);

result = kzcMemoryManagerDelete(systemMemoryManager);
kzsErrorForward(result);

See also

Using memory mapped loading

Using memory managers

Debugging application memory

Memory management best practices

Measuring the performance of your Kanzi application

Best practices